package com.hbsd.paopaobackend.controller;

import com.hbsd.paopaobackend.common.BaseResponse;
import com.hbsd.paopaobackend.common.ErrorCode;
import com.hbsd.paopaobackend.common.ResultUtils;
import com.hbsd.paopaobackend.common.request.DeleteRequest;
import com.hbsd.paopaobackend.exception.BusinessException;
import com.hbsd.paopaobackend.model.domain.User;
import com.hbsd.paopaobackend.model.request.PageInfo;
import com.hbsd.paopaobackend.model.request.Tag.AddTagRequest;
import com.hbsd.paopaobackend.model.request.Tag.UpdateChildrenTagRequest;
import com.hbsd.paopaobackend.service.TagService;
import com.hbsd.paopaobackend.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.hbsd.paopaobackend.constant.UserConstant.ADMIN_ROLE;

@Api(tags = "标签管理接口")
@RestController
@RequestMapping("/tags")
// 配置跨域 allowCredentials: 允许携带cookie
@CrossOrigin(origins = {"http://localhost:5173"}, allowCredentials = "true")
public class TagController {

    @Resource
    private UserService userService;
    @Resource
    private TagService tagService;


    /**
     * 添加标签
     * @param addTagRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "添加标签", notes = "添加标签 addTag")
    @PostMapping("/add")
    public BaseResponse<Boolean> addTag(@RequestBody AddTagRequest addTagRequest, HttpServletRequest request) {
        if(addTagRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        User loginUser = userService.getLoginUser(request);
        if(loginUser == null) {
            throw new BusinessException(ErrorCode.NO_AUTH, "请先登录");
        }

        Boolean result = tagService.addTag(addTagRequest, loginUser);
        return ResultUtils.success(result);
    }


    /**
     * 获取标签列表
     * @param pageInfo
     * @return
     */
    @ApiOperation(value = "获取标签列表", notes = "获取标签列表 listPage")
    @PostMapping("/list")
    public BaseResponse<List<Map<String, Object>>> listPage(@RequestBody PageInfo pageInfo) {
        if (pageInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分页参数错误");
        }

        List<Map<String, Object>> organizedTags = tagService.GettagList(pageInfo);



        return ResultUtils.success(organizedTags);
    }

    /**
     * 修改标签
     * @param updateChildrenTagRequests
     * @param request
     * @return
     */
    @ApiOperation(value = "更新标签", notes = "更新标签 updateTag")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTag(@RequestBody UpdateChildrenTagRequest[] updateChildrenTagRequests, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Integer userRole = loginUser.getUserRole();
        boolean isAdmin = userService.isAdmin(request);

        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NO_AUTH, "请先登录");
        }

        if (!isAdmin && userRole != ADMIN_ROLE) {
            throw new BusinessException(ErrorCode.NO_AUTH, "无权限修改标签");
        }

        boolean result = tagService.updateTagsBatch(updateChildrenTagRequests);
        return ResultUtils.success(result);
    }


    /**
     * 删除标签
     * @param deleteRequest
     * @param request
     * @return
     */
    @ApiOperation(value = "删除标签", notes = "删除标签 deleteTag")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTag(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Integer userRole = loginUser.getUserRole();
        boolean isAdmin = userService.isAdmin(request);

        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NO_AUTH, "请先登录");
        }

        if (!isAdmin && userRole != ADMIN_ROLE) {
            throw new BusinessException(ErrorCode.NO_AUTH, "无权限删除标签");
        }

        boolean result = tagService.deleteTag(deleteRequest);
        return ResultUtils.success(result);
    }


}
